package ObjectPackage;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class conversion {

	 /**
	 * Parse String to Time in format hh:mm.
     *@Parameter String
     *@Return String 
     */
	static String parseStringToTime(String strDate)
	{

		Calendar calendar = null;
   	    if (strDate != null) {
    	        try {
    	            DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm");
    	            Date date = formatter.parse(strDate);
    	            calendar = Calendar.getInstance();
    	            calendar.setTime(date);
    	        } catch (Exception e) {
    	            e.printStackTrace();
    	        }
    	    }
   	   return Integer.toString(calendar.get(Calendar.HOUR_OF_DAY))+":"+(calendar.get(Calendar.MINUTE)>9?Integer.toString(calendar.get(Calendar.MINUTE)):"0"+Integer.toString(calendar.get(Calendar.MINUTE)));
	}
	
	 /**
     * Parse String to Date in format yyyy.MM.dd.
     *
     * @param  str - the string to convert
     * @return      null - if the string is empty or there was an error
     *                      Date - if the conversion was successful
     */
    public static Calendar parseStringToDate(String strDate){
    	 Calendar cal = null;
   	    if (strDate != null) {
    	        try {
    	            DateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
    	            Date date = formatter.parse(strDate);
    	            cal = Calendar.getInstance();
    	            cal.setTime(date);
    	        } catch (Exception e) {
    	            System.out.println("Error with format!");
    	        }
    	    }

    	    return cal;
    }
    /**
     * Parse Date to String in format yyyy.MM.dd.
     *
     * @param  d - the date to convert
     * @return the string in format yyyy.MM.dd
     *                 empty string if d is null
     */
    public static String parseCalendarToString(Calendar aCalendar){
           
            return Integer.toString(aCalendar.get(Calendar.YEAR))+"."+((aCalendar.get(Calendar.MONTH)+1)>9?Integer.toString(aCalendar.get(Calendar.MONTH)+1):"0"+Integer.toString(aCalendar.get(Calendar.MONTH)+1))+"."+(aCalendar.get(Calendar.DATE)>9?Integer.toString(aCalendar.get(Calendar.DATE)):"0"+Integer.toString(aCalendar.get(Calendar.DATE)));
    }
	
}